#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QMessageBox>
#include<QLabel>
#include<QHBoxLayout>
#include"logoffwarning.h"
#include"tcpclient.h"
#include"modifyusrinfo.h"
#include"groupchat.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/img/application.png"));
    //默认刷新提示
    ui->actionRefresh->setToolTip("刷新好友列表");
    //返回上一层文件夹
    ui->actionFileBack->setVisible(false);
    //新建文件夹
    ui->actionFileNewFolder->setVisible(false);
    //上传文件
    ui->actionFileFiUpload->setVisible(false);
    //下载文件
    ui->actionFileDownloadFile->setVisible(false);
    //删除文件_文件夹
    ui->actionFileDelete->setVisible(false);
    //重命名文件_文件夹
    ui->actionFileRename->setVisible(false);
    //移动文件
    ui->actionFileMove->setVisible(false);
    //选择目标路径
    ui->actionTargetFile->setVisible(false);
    //分享文件
    ui->actionShareFile->setVisible(false);

    //私聊
    ui->actionPrivateChat->setVisible(true);
    //删除好友
    ui->actionDeleteFriend->setVisible(true);

    //查找用户
    ui->actionSearchUsr->setVisible(true);
    //消息广播
    ui->actionMesBroadCast->setVisible(true);
    m_friend=new Friend;
    m_fileSystem=new FileSystem;

    connect(m_fileSystem,&FileSystem::setStatus,this,&MainWindow::setTargetStatus);

    ui->stackedWidget->addWidget(m_friend);
    ui->stackedWidget->addWidget(m_fileSystem);

    //进度条dialog
    m_progressBar=new FileProgressBar;

    connect(m_progressBar,&FileProgressBar::showTip,this,&MainWindow::setStatusBarMsg);
}

MainWindow::~MainWindow()
{
    if(m_progressBar){
        delete m_progressBar;
        m_progressBar=nullptr;
    }
    groupChat::release();
    delete ui;
}


Friend *MainWindow::getFriend()
{
    return m_friend;
}

FileSystem *MainWindow::getFileSystem()
{
    return m_fileSystem;
}

void MainWindow::setTitle(QString name)
{
    setWindowTitle(name);

}


void MainWindow::setTargetStatus(bool status)
{
    if(status){
        ui->actionTargetFile->setEnabled(true);
    }else{
        ui->actionTargetFile->setEnabled(false);
    }
}


//选中好友系统
void MainWindow::on_actionFriendSystem_triggered()
{
    ui->actionRefresh->setToolTip("刷新好友列表");
    ui->stackedWidget->setCurrentIndex(0);
    ui->actionFileBack->setVisible(false);
    ui->actionFileNewFolder->setVisible(false);
    ui->actionFileFiUpload->setVisible(false);
    ui->actionFileDownloadFile->setVisible(false);
    ui->actionFileDelete->setVisible(false);
    ui->actionFileRename->setVisible(false);
    ui->actionFileMove->setVisible(false);
    ui->actionTargetFile->setVisible(false);
    ui->actionShareFile->setVisible(false);

    ui->actionPrivateChat->setVisible(true);
    ui->actionDeleteFriend->setVisible(true);
    ui->actionSearchUsr->setVisible(true);
    ui->actionMesBroadCast->setVisible(true);

    //刷新好友列表
    m_friend->flushFriendList();
}

//悬浮好友系统
void MainWindow::on_actionFriendSystem_hovered()
{
    ui->statusBar->showMessage("好友系统",2000);
}
//选中文件系统
void MainWindow::on_actionFileSystem_triggered()
{
    ui->actionRefresh->setToolTip("刷新文件列表");
    ui->stackedWidget->setCurrentIndex(1);
    ui->actionFileBack->setVisible(true);
    ui->actionFileNewFolder->setVisible(true);
    ui->actionFileFiUpload->setVisible(true);
    ui->actionFileDownloadFile->setVisible(true);
    ui->actionFileDelete->setVisible(true);
    ui->actionFileRename->setVisible(true);
    ui->actionFileMove->setVisible(true);
    ui->actionTargetFile->setVisible(true);
    ui->actionTargetFile->setEnabled(false);
    ui->actionShareFile->setVisible(true);

    ui->actionPrivateChat->setVisible(false);
    ui->actionDeleteFriend->setVisible(false);
    ui->actionSearchUsr->setVisible(false);
    ui->actionMesBroadCast->setVisible(false);

    //刷新文件列表
    m_fileSystem->flushDir();
}

//悬浮文件系统
void MainWindow::on_actionFileSystem_hovered()
{
    ui->statusBar->showMessage("文件系统",2000);
}

/**
 * ************************以下为通用按钮操作****************************************
 */

//刷新(好友列表/文件夹)
void MainWindow::on_actionRefresh_triggered()
{
    if(ui->stackedWidget->currentIndex()==0){
        m_friend->flushFriendList();
    }else if(ui->stackedWidget->currentIndex()==1){
        m_fileSystem->flushDir();
    }
}
//悬浮刷新按钮
void MainWindow::on_actionRefresh_hovered()
{
    if(ui->stackedWidget->currentIndex()==0){
        ui->statusBar->showMessage("刷新好友列表",2000);
    }else if(ui->stackedWidget->currentIndex()==1){
        ui->statusBar->showMessage("刷新文件列表",2000);
    }
}

//退出账号
void MainWindow::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    emit backToLogIn();
}
//设置状态栏消息(不消失)
void MainWindow::setStatusBarMsg(const QString &text)
{
    ui->statusBar->showMessage(text);
}

//退出账号
void MainWindow::on_actionexit_triggered()
{
    emit backToLogIn();
}
//悬浮退出账号
void MainWindow::on_actionexit_hovered()
{
    ui->statusBar->showMessage("退出账号",2000);
}
//注销账号
void MainWindow::on_actionlogOff_triggered()
{
    LogOffWarning*warning=new LogOffWarning;
    connect(warning,&LogOffWarning::logOffSignal,this,[](){
        TcpClient::getInstance().usrLogOff();
    });
    warning->show();
}
//悬浮注销账号
void MainWindow::on_actionlogOff_hovered()
{
    ui->statusBar->showMessage("注销账号",2000);
}

void MainWindow::setProgressBarTip(const QString &text)
{
    m_progressBar->setProgressBar(text);
}

void MainWindow::setProgressValue(int value)
{
    m_progressBar->setProgressValue(value);
}

void MainWindow::showProgressBar()
{
    m_progressBar->show();
}

void MainWindow::hideProgressBar()
{
    m_progressBar->hide();
}

void MainWindow::setCloseEnabled(bool flag)
{
    m_progressBar->setCloseBtnEnabled(flag);
}

void MainWindow::logInSet()
{
    ui->actionRefresh->setToolTip("刷新好友列表");
    ui->stackedWidget->setCurrentIndex(0);
    ui->actionFileBack->setVisible(false);
    ui->actionFileNewFolder->setVisible(false);
    ui->actionFileFiUpload->setVisible(false);
    ui->actionFileDownloadFile->setVisible(false);
    ui->actionFileDelete->setVisible(false);
    ui->actionFileRename->setVisible(false);
    ui->actionFileMove->setVisible(false);
    ui->actionTargetFile->setVisible(false);
    ui->actionShareFile->setVisible(false);

    ui->actionPrivateChat->setVisible(true);
    ui->actionDeleteFriend->setVisible(true);
    ui->actionSearchUsr->setVisible(true);
    ui->actionMesBroadCast->setVisible(true);

    //刷新好友列表
    m_friend->flushFriendList();
}

/**
 * ************************以下为好友系统相关操作****************************************
 */

//私聊
void MainWindow::on_actionPrivateChat_triggered()
{
    m_friend->sendPrivateChat();
}

//删除好友
void MainWindow::on_actionDeleteFriend_triggered()
{
    m_friend->deleteFriend();
}

// //查看在线用户
// void MainWindow::on_actionViewOnlineUsrs_triggered()
// {
//     m_friend->showAllUsrs();
// }

//查找用户
void MainWindow::on_actionSearchUsr_triggered()
{
    m_friend->searchUsrs();
}

//消息广播
void MainWindow::on_actionMesBroadCast_triggered()
{
    m_friend->mesBroadCast();
}

//悬浮私聊好友
void MainWindow::on_actionPrivateChat_hovered()
{
    ui->statusBar->showMessage("私聊好友",2000);
}

//悬浮删除好友
void MainWindow::on_actionDeleteFriend_hovered()
{
    ui->statusBar->showMessage("删除好友",2000);
}

// //悬浮查看在线用户
// void MainWindow::on_actionViewOnlineUsrs_hovered()
// {
//     ui->statusBar->showMessage("查看在线用户",2000);
// }

//悬浮查找好友
void MainWindow::on_actionSearchUsr_hovered()
{
    ui->statusBar->showMessage("查找好友",2000);
}

//悬浮消息广播
void MainWindow::on_actionMesBroadCast_hovered()
{
    ui->statusBar->showMessage("消息广播",2000);
}

/**
 * ************************以下为文件系统相关操作****************************************
 */
//返回上一层文件夹
void MainWindow::on_actionFileBack_triggered()
{
    m_fileSystem->backDir();
}

//悬浮返回上一级文件夹
void MainWindow::on_actionFileBack_hovered()
{
    ui->statusBar->showMessage("返回上一级文件夹",2000);
}

//新建文件夹
void MainWindow::on_actionFileNewFolder_triggered()
{
    m_fileSystem->createDir();
}
//悬浮新建文件夹
void MainWindow::on_actionFileNewFolder_hovered()
{
    ui->statusBar->showMessage("新建文件夹",2000);
}

//上传文件
void MainWindow::on_actionFileFiUpload_triggered()
{
    m_fileSystem->uploadFile();
}

//文件下载
void MainWindow::on_actionFileDownloadFile_triggered()
{
    m_fileSystem->downLoadFile();
}


//删除文件_文件夹
void MainWindow::on_actionFileDelete_triggered()
{
    m_fileSystem->delDir_File();
}

//悬浮文件上传
void MainWindow::on_actionFileFiUpload_hovered()
{
    ui->statusBar->showMessage("文件上传",2000);
}

//悬浮文件下载
void MainWindow::on_actionFileDownloadFile_hovered()
{
    ui->statusBar->showMessage("文件下载",2000);
}

//悬浮删除文件_文件夹
void MainWindow::on_actionFileDelete_hovered()
{
    ui->statusBar->showMessage("删除文件/文件夹",2000);
}

//重命名文件_文件夹
void MainWindow::on_actionFileRename_triggered()
{
    m_fileSystem->renameDir_File();
}
//悬浮重命名文件/文件夹
void MainWindow::on_actionFileRename_hovered()
{
    ui->statusBar->showMessage("重命名文件/文件夹",2000);
}

//移动文件
void MainWindow::on_actionFileMove_triggered()
{
    m_fileSystem->moveFile();
}

//悬浮移动文件
void MainWindow::on_actionFileMove_hovered()
{
    ui->statusBar->showMessage("移动文件/文件夹",2000);
}

//设置待移动文件的目标路径
void MainWindow::on_actionTargetFile_triggered()
{
    m_fileSystem->selectTargetPath();
}

//悬浮选中移动目标路径
void MainWindow::on_actionTargetFile_hovered()
{
    ui->statusBar->showMessage("选择待移动文件/文件夹目标路径",2000);
}




void MainWindow::on_actionShareFile_triggered()
{
    m_fileSystem->shareFile();
}


void MainWindow::on_actionShareFile_hovered()
{
    ui->statusBar->showMessage("分享文件_文件夹",2000);
}


void MainWindow::on_actionupdateUsrMes_triggered()
{
    QString usrName=TcpClient::getInstance().usrName();
    QString password=TcpClient::getInstance().usrPassword();

    ModifyUsrInfo *modifyDlg=new ModifyUsrInfo(usrName,password);
    modifyDlg->show();

    connect(modifyDlg,&ModifyUsrInfo::sendModifyNewItems,this,[=](const QString&newName,const QString&newPwd){
        TcpClient::getInstance().modifyUsrInfo(usrName,newName,newPwd);
    });
}


void MainWindow::on_actionupdateUsrMes_hovered()
{
    ui->statusBar->showMessage("修改用户信息",2000);
}

